package com.example.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.example.common.api.ApiResult;
import com.example.entity.emp.SysDept;
import com.example.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "部门管理接口")
@RestController
@RequestMapping("/dept")
@RequiredArgsConstructor
public class SysDeptController {

    private final SysDeptService deptService;

    @GetMapping("/tree")
    @ApiOperation("获取部门树形列表")
    public ApiResult<List<SysDept>> tree() {
        return ApiResult.success(deptService.getDeptTree());
    }

    @PostMapping
    @ApiOperation("新增部门")
    @SaCheckRole("ROLE_ADMIN")
    public ApiResult<Void> add(@Validated @RequestBody SysDept dept) {
        deptService.add(dept);
        return ApiResult.success();
    }

    @PutMapping
    @ApiOperation("修改部门")
    @SaCheckRole("ROLE_ADMIN")
    public ApiResult<Void> update(@Validated @RequestBody SysDept dept) {
        deptService.update(dept);
        return ApiResult.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除部门")
    @SaCheckRole("ROLE_ADMIN")
    public ApiResult<Void> delete(@PathVariable Long id) {
        deptService.delete(id);
        return ApiResult.success();
    }

    @GetMapping("/children/{deptId}")
    @ApiOperation("获取子部门ID列表")
    public ApiResult<List<Long>> getChildrenIds(@PathVariable Long deptId) {
        return ApiResult.success(deptService.getChildrenIds(deptId));
    }
} 